//
//  SYBrandViewController.m
//  TermOfSectionB
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYBrandViewController.h"
#import "SYBrandCollectionViewCell.h"
#import "SYCommodityViewController.h"

#define SWIDTH [UIScreen mainScreen].bounds.size.width / 375.0
#define SHEIGHT [UIScreen mainScreen].bounds.size.height / 667.0

@interface SYBrandViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *myCollectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, assign)NSInteger page;

@end

@implementation SYBrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor purpleColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(170 * SWIDTH, 200 * SHEIGHT);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 601 * SHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    
    
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[SYBrandCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    
    self.myCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
    [self createData];

    
}

- (void)footerAction {
    
    self.page = 0;
    self.page++;
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/search/list" POST:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", self.brandId], @"brandId", @"-1", @"fromType", @"2", @"imageHD", @"8ba3cfaa4de658532d805e3a416d49b8", @"key", [NSString stringWithFormat:@"%ld", self.page], @"page", @"1446541744776", @"t", @"705811", @"userid", @"3.5.4", @"version", nil] block:^(id result) {
        
        NSMutableArray *arArr = [NSMutableArray array];
        arArr = [CLsame baseModelByArr:result[@"data"][@"records"]];
        for (CLsame *same in arArr) {
            [self.arr addObject:same];
        }
        [self.arr removeLastObject];
        [self.myCollectionView.footer endRefreshing];
        [self.myCollectionView reloadData];
        
    }];
}



- (void)createData {
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/search/list" POST:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", self.brandId], @"brandId", @"-1", @"fromType", @"2", @"imageHD", @"8ba3cfaa4de658532d805e3a416d49b8", @"key", @"0", @"page", @"1446541744776", @"t", @"705811", @"userid", @"3.5.4", @"version", nil] block:^(id result) {
        
        self.arr = [NSMutableArray array];
        self.arr = [CLsame baseModelByArr:result[@"data"][@"records"]];
        
        [self.myCollectionView reloadData];
        
    }];

    

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SYBrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    CLsame *same = self.arr[indexPath.row];
    
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:same.image]];
    cell.proLabel.text = same.brand;
    
    cell.addLabel.text = same.productDescription;
    
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SYCommodityViewController *intVC = [[SYCommodityViewController alloc] init];
    [self.navigationController pushViewController:intVC animated:YES];
    
    CLsame *same = self.arr[indexPath.row];
    
    intVC.tempID = same.productId;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
